<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<!--#include file="upload.inc.asp"-->
</head>

<body>
<%
dim upload,file,formName,formPath,filename,fileExt
dim ranNum
call UpFile()
'===========无组件上传(upload_0)====================
sub UpFile()
set upload=new UpFile_Class '建立上传对象
upload.GetData (1024*1024) '取得上传数据,此处即为1 MB

if upload.err > 0 then
select case upload.err
case 1
Response.Write "请先选择你要上传的文件　[ <a href=# onclick=history.go(-1)>重新上传</a> ]"
case 2
Response.Write "图片大小超过了限制 1024 K　[ <a href=# onclick=history.go(-1)>重新上传</a> ]"
end select
exit sub
else
formPath=upload.form("filepath") '文件保存目录,此目录必须为程序可读写
if formPath="" then
formPath="upload/"
end if
'在目录后加(/)
if right(formPath,1)<>"/" then 
formPath=formPath&"/"
end if
for each formName in upload.file '列出所有上传了的文件
set file=upload.file(formName) '生成一个文件对象
if file.filesize<100 then
response.write "请先选择你要上传的图片　[ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if

fileExt=lcase(file.FileExt)
if CheckFileExt(fileEXT)=false then
response.write "文件格式不正确　[ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if

randomize
ranNum=int(90000*rnd)+10000
filename=formPath&year(now)&month(now)&day(now)&hour(now)&ranNum&"."&fileExt
if file.FileSize>0 then '如果 FileSize > 0 说明有文件数据
result=file.SaveToFile(Server.mappath(filename)) '保存文件
if result="ok" then
response.write formName&" upload OK, had saved to "&filename&"<br>"
	set conn=server.createobject("adodb.connection")
	conn.open "provider=microsoft.jet.oledb.4.0;data source="&server.mappath("App_Data\MainDB.mdb")
	set rs=server.createobject("adodb.recordset")
	rs.open "SELECT * FROM [Species] WHERE SpeciesID=" & request.QueryString("id"),conn,3,3
	if len(rs("filepath"))=0 or isnull(rs("filepath")) then
		rs("filepath")=filename
		rs.update
	end if
	rs.close
	rs.open "SELECT * From [Pics]",conn,3,3
	rs.addnew
	rs("PicURL")=filename
	rs("SpeciesID")=request.QueryString("id")
	rs.update
	rs.close
	set rs=nothing
	conn.close
	set conn=nothing
else
response.write formName&" upload Fail,"&result&"<br>"
end if
end if
set file=nothing
next
set upload=nothing
end if
end sub

'判断文件类型是否合格
Private Function CheckFileExt (fileEXT)
dim Forumupload
Forumupload="gif,jpg,bmp,jpeg"
Forumupload=split(Forumupload,",")
for i=0 to ubound(Forumupload)
if lcase(fileEXT)=lcase(trim(Forumupload(i))) then
CheckFileExt=true
exit Function
else
CheckFileExt=false
end if
next
End Function
%>
</body>
</html>
